package smecta.client.print.service;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import smecta.client.print.domain.ScpCodeFormModel;
import smecta.client.print.mapper.ScpCodeFormMapper;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class ScpCodeFormService extends BaseService<ScpCodeFormModel> {

    @Autowired
    private ScpCodeFormMapper scpCodeFormMapper;

    public ScpCodeFormModel findByCodeNum(String codeNum) {
        if (StringUtils.isBlank(codeNum)){
            return new ScpCodeFormModel();
        }
        Example example = new Example(ScpCodeFormModel.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("codeNum",codeNum);
        return selectOneByExample(example);
    }

    public String getMaxCodeNum(String codeNum) {
        return scpCodeFormMapper.getMaxCodeNum(codeNum);
    }

    public String getMaxRealCodeNum(String codeNum) {
        return scpCodeFormMapper.getMaxRealCodeNum(codeNum);
    }

    @Transactional(rollbackFor = Exception.class)
    public void updateStatusByCodeNum(String codeNum,Integer status) {
        ScpCodeFormModel byCodeNum = findByCodeNum(codeNum);
        ScpCodeFormModel model = new ScpCodeFormModel();
        model.setId(byCodeNum.getId());
        model.setStatus(status);
        updateByPrimaryKeySelective(model);
    }

    public List<ScpCodeFormModel> getAllListByStatus(Integer status){
        List<ScpCodeFormModel> list = selectAll();
        if (status == null){
            return list;
        }
        return list.stream().filter(o-> o.getStatus().equals(status)).collect(Collectors.toList());
    }
}
